package com.marquee.signup;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.marquee.R;

public class Act_Input_Number extends Activity {

	static String LOG_TAG = "test";
	
	EditText ET_input_phone_number;
	Button BT_submit_phone_number;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.signup_activity_input_number);
	    
	    ET_input_phone_number = (EditText)findViewById(R.id.ET_input_phone_number);
	    BT_submit_phone_number = (Button)findViewById(R.id.BT_submit_phone_number);

	    ET_input_phone_number.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			public void afterTextChanged(Editable s) {
				
				if(s.length()>=11){
					
					BT_submit_phone_number.setEnabled(true);
				}
				// 아무 입력글자가 없을경우
				else{
					BT_submit_phone_number.setEnabled(false);
				}
				
			}
		});
	    
	    BT_submit_phone_number.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(v.isEnabled()){
					
					String input_PhoneNumber = ET_input_phone_number.getText().toString();
					
					Resources res = getResources();
					String text = String.format(res.getString(R.string.message_for_auth_phone_number), input_PhoneNumber);
					
					Builder d = new AlertDialog.Builder(Act_Input_Number.this);
					d.setMessage(text);
					d.setPositiveButton("전송", new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							
							String auth_Number = Integer.toString((int)(Math.random()*1000000000)*1000000);
							auth_Number = auth_Number.substring(1, 5);
							Log.d(LOG_TAG, "auth_Number: "+auth_Number);
							
							String phoneNumber = ET_input_phone_number.getText().toString();
							final SmsManager sms = SmsManager.getDefault();
							sms.sendTextMessage( phoneNumber, null, "["+auth_Number+"]소셜클럽에서 보낸 인증번호입니다.", null, null);
							

							
						
							Intent i = new Intent(Act_Input_Number.this, Act_Sms_Auth.class);
							
							Intent intent = getIntent();
							Bundle b = intent.getExtras();
							
							if(b!=null){
							i.putExtras(b);
							}
							i.putExtra("auth_Number",  auth_Number);
							i.putExtra("phoneNumber",  phoneNumber);
							startActivity(i);
							 overridePendingTransition(R.anim.fade,R.anim.hold);
						}
					});
					d.setNegativeButton("취소", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							
							dialog.cancel();

						}
					});
					d.show();
					
					
					
					
					
					
					
				}
				
			}
		});
		
	}

}
